package com.hz.eduService.controller;

import com.hz.apiService.model.request.ApiResponse;
import com.hz.eduService.domain.Course;
import com.hz.eduService.domain.UserCourse;
import com.hz.eduService.request.EditCourseRequest;
import com.hz.eduService.service.CourseService;
import com.hz.eduService.service.UserCourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LiiiYiAn
 * @version 1.0
 * @createDate 2023-09-23 20:22
 * @projectName SchoolServicePlatformFinal
 * @className EduManagerController
 * @description 控制器 - 课程管理
 */
@RestController
@RequestMapping("/edu-manager")
@RequiredArgsConstructor  // 使用构造器自动注入
public class EduManagerController {

    private final CourseService courseService;


    /**
     * @return java.lang.String
     * @author LiiiYiAn
     * @createDate 2023/9/23 21:10
     * @description 测试网关服务与公共服务
     **/
    @GetMapping("/test")
    public String course() {
        return "国服马超的微服务架构";
    }

    @PostMapping("/add-course")
    public String addCourse(@RequestParam("courseName") String courseName, @RequestParam("courseCode") String courseCode,
                            @RequestParam("teacherId") Integer teacherId,
                            @RequestParam("sectionCount") Integer sectionCount, @RequestParam("courseTime") String courseTime,
                            @RequestParam("maxStudentCount") Integer maxStudentCount, @RequestParam("courseProfile") String courseProfile,
                            @RequestParam("textbook") String textbook, @RequestParam("creditsCount") Integer creditsCount,
                            @RequestParam("allScore") Integer allScore, @RequestParam("passScore") Integer passScore,
                            @RequestParam("isDelete") Integer isDelete) {
        int add = courseService.addCourse(courseName, courseCode, teacherId, sectionCount, courseTime, maxStudentCount, courseProfile, textbook, creditsCount, allScore, passScore, isDelete);
        return add > 0 ? ApiResponse.ok(add) : ApiResponse.intError();
    }

    @PostMapping("/edit-course")
    public String editCourse(@RequestBody EditCourseRequest editCourseRequest
    ) {
        Course course = new Course();
        course.setId(editCourseRequest.getId());
        course.setCourseCode(editCourseRequest.getCourseCode());
        course.setCourseName(editCourseRequest.getCourseName());
        course.setTeacherUserId(editCourseRequest.getTeacherUserId());
        course.setCourseProfile(editCourseRequest.getCourseProfile());
        course.setSectionCount(editCourseRequest.getSectionCount());
        course.setCourseTime(editCourseRequest.getCourseTime());
        course.setAllScore(editCourseRequest.getAllScore());
        course.setPassScore(editCourseRequest.getPassScore());

        return courseService.editCourse(course) ? ApiResponse.ok(null) : ApiResponse.intError();
    }

    @PostMapping("/delete-course")
    public String deleteCourse(@RequestParam("id") Integer id) {

        return courseService.deleteCourse(id) ? ApiResponse.ok(null) : ApiResponse.intError();
    }

    @PostMapping("/view-course")
    public String viewCourse(@RequestParam("id") Integer id) {
        Course course = courseService.viewCourse(id);
        return course != null ? ApiResponse.ok(course) : ApiResponse.intError();
    }

    @Resource
    UserCourseService userCourseService;

    @PostMapping("/view-selected-student")
    public String viewSelectStudent(@RequestParam("courseId") Integer id) {
        List<UserCourse> userCourses = userCourseService.viewSelectedStudents(id);
        return !userCourses.isEmpty() ? ApiResponse.ok(userCourses) : ApiResponse.intError();
    }

    @PostMapping("/check-score")
    public String checkScore(@RequestParam("id") Integer id,
                             @RequestParam("userScore") Integer userScore,
                             @RequestParam("courseId") Integer courseId) {
        UserCourse userCourse = userCourseService.checkScore(id, userScore, courseId);
        return userCourse != null ? ApiResponse.ok(userCourse) : ApiResponse.intError();
    }
}